import os
import os.path
from itertools import groupby
import glob
import time
from n2 import HnswIndex
import numpy as np
import json
import urllib3
import redis
import pymysql

pp1 = '/data1/auto_add/vcgCreative_data/vcgCreative_n2_ready/'
pp2 = '/data1/auto_add/vcgCreative_data/vcgCreative_csv_ready/'
#VCG 编辑类 and 创意类数据库连接
def open_db_ec():
	global db, cursor
	db = pymysql.connect("rr-2ze082xx4x2rr1er2.mysql.rds.aliyuncs.com", "javaapp", "javaappA_j", "resource23")
	cursor = db.cursor()

def close_db_ec():
	global db
	db.close()
#Veer 数据库连接
def open_db_veer():
	global db1, cursor1
	db1 = pymysql.connect("rm-2ze3s4g435je13i04.mysql.rds.aliyuncs.com", "istock", "wrG7cDDXLG4gbq", "istock")
	cursor1 = db1.cursor()


def close_db_veer():
	global db1
	db1.close()


myPath = '/data1/auto_add/vcgCreative_data/vcgCreative_n2_ready/'



# 获取目录下的所有文件
def getallfile(path):
	allfile = []
	allfilelist = os.listdir(path)
	for file in allfilelist:
		filepath = os.path.join(path, file)
		allfile.append(filepath)
	return allfile

# 获取文件名
def getFilename(filename):
	(filepath, tempfilename) = os.path.split(filename)
	(shotname, extension) = os.path.splitext(tempfilename)
	return shotname

# 获取扩展名
def getFileExtension(filename):
	(filepath, tempfilename) = os.path.split(filename)
	(shotname, extension) = os.path.splitext(tempfilename)
	return extension

# 获取目录下所有文件，返回一个列表
allcsvfiles = getallfile(myPath)
print(allcsvfiles)
n2_list = []

# 获取目录下所有n2文件
for item in allcsvfiles:
	# print(item)
	ext = str(getFileExtension(item))
	# print(ext)
	filename = str(getFilename(item))
	# print(filename)

	if ext == '.n2':
		filenames = filename + '.n2'
		n2_list.append(filenames)
print(n2_list)


# 生成时间目录
def get_filename():
	# 获得当前系统时间的字符串
	localtime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
	print('localtime=' + localtime)
	# 系统当前时间年份
	year = time.strftime('%Y', time.localtime(time.time()))
	# 月份
	month = time.strftime('%m', time.localtime(time.time()))
	# 日期
	day = time.strftime('%d', time.localtime(time.time()))
	# 具体时间 小时分钟毫秒
	mdhms = time.strftime('%m%d%H%M%S', time.localtime(time.time()))

	fileYear = '/data1/auto_add/' + year  # 这里可以定义目录
	fileMonth = fileYear + '/' + month
	fileDay = fileMonth + '/' + day

	if not os.path.exists(fileYear):
		os.mkdir(fileYear)
		os.mkdir(fileMonth)
		os.mkdir(fileDay)
	else:
		if not os.path.exists(fileMonth):
			os.mkdir(fileMonth)
			os.mkdir(fileDay)
		else:
			if not os.path.exists(fileDay):
				os.mkdir(fileDay)

	# 创建一个文件，以‘timeFile_’+具体时间为文件名称
	# fileDir = fileDay + '/timeFile_' + mdhms + '.txt'
	return  fileDay



# 随机选取300个对比
def check(sucesslogname):
	oldf = open(sucesslogname, 'r')
	lines = oldf.readlines()
	total = len(lines)
	# 计算总数
	if total > 300:
		print(total)
		# 选取前100
		list1 = [i for i in range(1, 101)]
		# print(list1)
		# 选取中间100
		list2 = [i for i in range(total // 2 - 50, total // 2 + 50)]
		# print(list2)
		# 选取最后100
		list3 = [i for i in range(total - 99, total + 1)]
		# print(list3)
		# print(len(list3))
		# 拼接
		list5 = list1 + list2 + list3
		# print(list5)
		return list5
	# 日志总数小于300
	else:
		print(total)
		# 选取第一个
		list1 = [1]
		print(list1)
		# 中间的
		list2 = [total // 2]
		print(list2)
		# 选取最后100
		list3 = [total ]
		print(list3)
		print(len(list3))
		# 拼接
		list5 = list1 + list2 + list3
		print(list5)

		return list5



#***************第一步生成目录*************
pre_path= get_filename()
print(pre_path)
localtime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))

# 遍历列表
for n2 in n2_list:
	filePath = n2

	print(filePath)
	m = [''.join(g) for _, g in groupby(filePath, key=lambda x: x.isdigit() * 'd' or x.isalpha() * 'a')]
	filesort = m[0]
	file_num = int(m[1])
	heapNumber = file_num // 2
	if filesort == 'editvcg':
		sort = 1
		assetFamily = 1
	elif filesort == 'vcgCreativeIndex':
		sort = 1
		assetFamily = 2
	elif filesort == 'veerlog':
		sort = 3
		assetFamily = 2
	print(sort, assetFamily)

	# print(filesort)
	# print(file_num)
	# print(type(file_num))
	# print(type(assetFamily))

	for i in glob.glob('/data1/auto_add/vcgCreative_data/vcgCreative_n2_ready/su*'):
		i = os.path.basename(os.path.realpath(i))
		print(i)

		a = i.split('.', 1)[0]
		print(a)
		s_sort = int(i.split('_')[1])
		print(s_sort)
		s_assetFamily = int(i.split('_')[2])
		s_num = int(a.split('_')[-1])
		# print(type(i))

		print(s_num)
		if assetFamily == s_assetFamily and file_num == s_num and sort == s_sort:
			print('匹配')
			check_list = check(i)
			with open(i, 'r') as fi:
				lines = fi.readlines()
			n = 0
			for i in check_list:
				n += 1
				print(n)
				# 索引位置要减1
				check_list_num = int(lines[i - 1])
				print(check_list_num)
				if sort == 1:
					open_db_ec()
					sql = """SELECT oss_400 from res_image WHERE id = %d""" % check_list_num
					# print(sql)
					try:
						# 执行sql语句

						cursor.execute(sql)
						# 获取所有记录列表
						picUrl1 = cursor.fetchone()[0]
						print(picUrl1)

					except:
						# 如果发生错误则回滚
						print("出错了")
						db.rollback()
					close_db_ec()

				elif sort == 3:
					assetFamily = 3  #************************这是一个非常不好的做法需要修改
					open_db_veer()
					sql = """SELECT  oss_400 from res_istock_image WHERE id = %d""" % check_list_num
					# print(sql)
					try:
						# 执行sql语句

						cursor1.execute(sql)
						# 获取所有记录列表
						picUrl1 = cursor1.fetchone()[0]
						print(picUrl1)

					except:
						# 如果发生错误则回滚
						print("出错了")

						db1.rollback()
					close_db_veer()

				f = 2048  # Weidu
				u1 = HnswIndex(f, "angular")
				u1.load(filePath, use_mmap=True)

				k = 100  # result of search (return k results)
				efSearchNum = 800

				r = redis.Redis(host='r-2zea3c0ff18d2e94.redis.rds.aliyuncs.com', port=6379, db=0,
				                password='b3bPVU8W',
				                decode_responses=True)
				# 线上端口是37现在
				picUrl = "http://172.16.237.37:5000/getfeatures?ossurl=" + 'http://bj-feiyuantu.oss-cn-beijing.aliyuncs.com/'  + picUrl1
				http = urllib3.PoolManager()
				req = http.request('GET', picUrl)
				vector = req.data.decode('UTF-8')
				# print(vector)
				# exit()
				print("%d:GPU done" % (int(time.time())))
				features_arr = vector.split(',')
				features_arr = [a for a in features_arr if len(a) > 0]
				features = np.array(features_arr, dtype=np.float64)
				try:
					nns = u1.search_by_vector(features, k, include_distances=True, ef_search=efSearchNum)
				# print(nns)
				except:
					print(False)
					exit()

				print("%d:n2 done" % (int(time.time())))

				result = int(nns[0][0])

				index_no = heapNumber
				index_id = nns[0][0]
				r_key = '%d_%s_%s' % (index_no, index_id, assetFamily)
				print(r_key)
				result_id = r.get(r_key)
				#在这里加一个判断判定结果是否一致
				#fileDir = fileDay + '/timeFile_' + mdhms + '.txt'
				errorlog =  pre_path + '/'+ localtime + '_' +str(sort) +'_' + str(assetFamily) +'_'+ str(file_num) + 'errorlog' + '.txt'
				sucesslog = pre_path + '/'+ localtime + '_' +str(sort) +'_' +  str(assetFamily)+'_' + str(file_num) +'sucesslog' + '.txt'
				current_errorlog = '/data1/auto_add/vcgCreative_data/vcgCreative_n2_ready/'+ str(file_num) +'_' +str(sort) +'_' + str(assetFamily) +'_' + 'errorlog' + '.txt'

				if check_list_num == int(result_id):
					with open(sucesslog, 'a') as ff:
						ff.write(str(check_list_num )+ '\n')
				else:
					with open(errorlog, 'a')  as  ff:
						ff.write(str(check_list_num) + ' ' + result_id + ' ' + r_key + '\n' )
					with open(current_errorlog, 'a') as fc:
						fc.write(str(check_list_num) + ' ' + result_id + ' ' + r_key + '\n')
				print("%d:redis done" % (int(time.time())))
				if (int(result_id) > 0):
					compare_info = {
						'result_id': result_id,
						'score': nns[0][1],
					}
					compare_info_json = json.dumps(compare_info)
					print(compare_info_json)
				else:
					print(False)
				print("%d:end" % (int(time.time())))